package com.java.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.business.dao.ComplaintMapper;
import com.java.business.model.Complaint;
import com.java.business.model.PurchaseBill;
import com.java.business.service.ComplaintService;
import com.java.common.model.Result;
import com.java.common.util.RedisUtil;
import com.java.manage.model.Company;
import com.java.manage.service.api.CompanyApiService;
import com.java.userservice.model.User;
import com.java.userservice.service.api.UserServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ComplaintServiceImpl implements ComplaintService {

    @Autowired
    private ComplaintMapper complaintMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @DubboReference
    private UserServiceApi userServiceApi;

    @DubboReference
    private CompanyApiService companyApiService;

    @Override
    public Result save(Complaint complaint) {
        complaintMapper.save(complaint);
        return Result.success();
    }

    @Override
    public Result modify(Complaint complaint) {
        complaintMapper.modify(complaint);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            complaintMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result list(Complaint complaint) {
        PageHelper.startPage(complaint.getPageNum(), complaint.getPageSize());

        List<Complaint> list = complaintMapper.list(complaint);

        for (Complaint com : list) {
            if (com.getUserId() != null){
                //关联用户表
                List<Integer> collectes = list.stream().map(Complaint::getUserId).collect(Collectors.toList());
                List<User> companie = userServiceApi.listByIds(collectes);
                Map<Integer, User> userMap = companie.stream().collect(Collectors.toMap(company -> company.getId(), company -> company));

                list.stream().forEach(bor -> {
                    bor.setUser(userMap.get(bor.getUserId()));
                });

                // 查询关联的公司信息
                List<Integer> storeCollect = list.stream().map(Complaint::getStoreId).collect(Collectors.toList());
                List<Company> companies = companyApiService.listByIds(storeCollect);
                Map<Integer, Company> companyMap = companies.stream().collect(Collectors.toMap(company -> company.getId(), company -> company));
                list.stream().forEach(app -> {
                    app.setCompany(companyMap.get(app.getStoreId()));
                });
            }
        }

        PageInfo<Complaint> pageInfo = new PageInfo(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return Result.success(map);
    }

    @Override
    public Result get(int id) {
        Complaint complaint = complaintMapper.get(id);
        return Result.success(complaint);
    }
}
